#include <stdio.h>
#include <stdlib.h>

// 单链表节点结构
struct ListNode {
    int val;
    struct ListNode* next;
};

// 创建新节点
struct ListNode* createNode(int value) {
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = value;
    newNode->next = NULL;
    return newNode;
}

// 创建链表
struct ListNode* createLinkedList() {
    return NULL;
}

// 在头部插入
void insertAtHead(struct ListNode** head, int value) {
    struct ListNode* newNode = createNode(value);
    newNode->next = *head;
    *head = newNode;
}

// 在尾部插入
void insertAtTail(struct ListNode** head, int value) {
    struct ListNode* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    struct ListNode* current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
}

// 在指定位置插入
void insertAtIndex(struct ListNode** head, int index, int value) {
    if (index < 0) {
        return;
    }
    if (index == 0) {
        insertAtHead(head, value);
        return;
    }
    struct ListNode* current = *head;
    for (int i = 0; i < index - 1 && current != NULL; i++) {
        current = current->next;
    }
    if (current == NULL) {
        return;
    }
    struct ListNode* newNode = createNode(value);
    newNode->next = current->next;
    current->next = newNode;
}

// 删除头部节点
void deleteAtHead(struct ListNode** head) {
    if (*head == NULL) {
        return;
    }
    struct ListNode* temp = *head;
    *head = (*head)->next;
    free(temp);
}

// 删除尾部节点
void deleteAtTail(struct ListNode** head) {
    if (*head == NULL) {
        return;
    }
    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }
    struct ListNode* current = *head;
    while (current->next->next != NULL) {
        current = current->next;
    }
    free(current->next);
    current->next = NULL;
}

// 删除指定位置的节点
void deleteAtIndex(struct ListNode** head, int index) {
    if (index < 0 || *head == NULL) {
        return;
    }
    if (index == 0) {
        deleteAtHead(head);
        return;
    }
    struct ListNode* current = *head;
    for (int i = 0; i < index - 1 && current != NULL; i++) {
        current = current->next;
    }
    if (current == NULL || current->next == NULL) {
        return;
    }
    struct ListNode* temp = current->next;
    current->next = temp->next;
    free(temp);
}

// 查找节点
struct ListNode* find(struct ListNode* head, int value) {
    struct ListNode* current = head;
    while (current != NULL) {
        if (current->val == value) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

// 获取链表长度
int getSize(struct ListNode* head) {
    int size = 0;
    struct ListNode* current = head;
    while (current != NULL) {
        size++;
        current = current->next;
    }
    return size;
}

// 遍历链表
void traverse(struct ListNode* head) {
    struct ListNode* current = head;
    while (current != NULL) {
        printf("%d ", current->val);
        current = current->next;
    }
    printf("\n");
}

// 清空链表
void clear(struct ListNode** head) {
    struct ListNode* current = *head;
    while (current != NULL) {
        struct ListNode* temp = current;
        current = current->next;
        free(temp);
    }
    *head = NULL;
}

// 主函数示例
int main() {
    struct ListNode* head = NULL;

    // 插入元素
    insertAtHead(&head, 1);
    insertAtTail(&head, 2);
    insertAtTail(&head, 3);

    // 遍历链表
    printf("链表内容: ");
    traverse(head);

    // 获取链表长度
    printf("链表长度: %d\n", getSize(head));

    // 查找元素
    struct ListNode* node = find(head, 2);
    if (node != NULL) {
        printf("找到元素: %d\n", node->val);
    }

    // 删除元素
    deleteAtTail(&head);
    printf("删除尾部元素后的链表: ");
    traverse(head);

    // 清空链表
    clear(&head);
    printf("清空链表后的链表: ");
    traverse(head);

    return 0;
}